<template>
  <div class="main">
    <home-header></home-header>
    <home-swiper :swiperList="swiperList"></home-swiper>
    <home-icons :iconList="iconList"></home-icons>
    <home-hot-sale :hotSaleList="hotSaleList"></home-hot-sale>
    <home-like-item :likeItemList="likeItemList"></home-like-item>
    <home-weekend :weekendList="weekendList"></home-weekend>
  </div>
</template>

<script>
import HomeHeader from '@/components/HomeHeader'
import HomeSwiper from '@/components/HomeSwiper'
import HomeIcons from '@/components/HomeIcons'
import HomeHotSale from '@/components/HomeHotSale'
import HomeLikeItem from '@/components/HomeLikeItem'
import HomeWeekend from '@/components/HomeWeekend'
import axios from 'axios'
import { mapState } from 'vuex'
export default {
  name: 'Home',
  components: {
    HomeHeader,
    HomeSwiper,
    HomeIcons,
    HomeHotSale,
    HomeLikeItem,
    HomeWeekend
  },
  data () {
    return {
      swiperList: [],
      iconList: [],
      hotSaleList: [],
      likeItemList: [],
      weekendList: [],
      lastCity: ''
    }
  },
  computed: {
    ...mapState([
      'city'
    ])
  },
  methods: {
    getInfo () {
      axios.get(`./api/index.json?city=${this.city}`)
        .then(this.getInfoSucc)
    },
    getInfoSucc (res) {
      res = res.data
      if (res.ret) {
        const data = res.data
        this.swiperList = data.swiperList
        this.iconList = data.iconList
        this.hotSaleList = data.hotSaleList
        this.likeItemList = data.likeItemList
        this.weekendList = data.weekendList
      }
    }
  },
  mounted () {
    this.getInfo()
    this.lastCity = this.city
  },
  activated () {
    if (this.lastCity !== this.city) {
      this.lastCity = this.city
      this.getInfo()
    }
  }
}
</script>

<style lang="stylus" scoped>
  .main
    background #f5f5f5
</style>
